<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            padding-bottom: 800px;
        }
        .box{
            border: 1px solid black;
            padding: 20px;
        }
        .switch-box{
            font-size: 12px;
            user-select: none;
        }
        .switch-box>span{
            font-size: 1.2em;
            vertical-align: middle;
            /* font-weight: bold; */
        }
        .switch-box>span.active{
            color: #409eff;
        }
        .switch-btn{
            vertical-align: middle;
            margin: 0px 0.5em;
            display: inline-block;
            /* 单位采用em */
            width: 4em;
            height: 2em;
            border-radius: 2em;
            cursor: pointer;
            background-color: #dcdfe6;
            box-shadow: 0 0 0.3em #999 inset;
            transition: all 400ms ease;
        }
        .switch-btn::after{
            content: "";
            display: inline-block;
            width: 2em;
            height: 2em;
            background-color: white;
            border-radius: 50%;
            transform: translate(0%,0%) scale(0.8);
            box-shadow: 0 0 0.3em #999;
            transition: all 400ms ease;
        }
        .switch-box input[type="checkbox"]{
            display: none;
        }
        .switch-box input[type="checkbox"]:checked + .switch-btn{
            background-color: #409eff;
            box-shadow: 0 0 0.3em #2c6fb1 inset;
        }
        .switch-box input[type="checkbox"]:checked + .switch-btn::after{
            transform: translate(100%,0%) scale(0.8);
        }
    </style>
</head>
<body>
    <div id="app" class="box">
        <h4>父组件</h4>
        <!-- 
            父向子进行数据传递：自定义属性的绑定和子组件的props拦截 

            vue2.0 对 子组件标签的 class 和 style 进行了特殊处理，这两个属性是不能被拦截的
            vue3.0 对 子组件标签的 class 和 style 是可以被拦截处理的
                ==> 上述限制，是为了将class和style作为组件根元素的样式进行直接写入绑定，
                     让组件可以实现根据需要动态调整元素布局

            vue2.0 组件定义时 模板元素必须保证有且仅有一个根节点
            vue3.0 组件定义时 模板元素无需提供唯一根节点
                   所以当前vue3.0的多根元素组件被定义时，组件标签传递了class和style时，需要手动完成绑定指向
        -->
        <p>msg:{{ msg }}</p>
        <comp-one :msg="msg" class="myClass" :style=" 'color:red' "></comp-one>
        <hr>
        <switch-btn></switch-btn>
        <br>
        <switch-btn style="font-size:16px"></switch-btn>
        <hr>
        <comp-two class="myClass" :msg="msg"></comp-two>
    </div>

    <script type="text/x-template" id="compOne">
        <div class="box">
            <h4>子组件</h4>
            <p>msg:{{ msg }}</p> 
        </div>
    </script>

    <script type="text/x-template" id="switchBtn">
        <label class="switch-box">
            <span v-bind:class=" switchState||'active' ">按年付费</span>
            <input type="checkbox" v-bind:checked="switchState" v-on:change="changeState( $event.target.checked )">
            <div class="switch-btn"></div>
            <span v-bind:class=" !switchState||'active' ">按月付费</span>
        </label>
    </script>

    <script type="text/x-template" id="compTwo">
        <!-- $attrs 是当前组件标签在页面使用时，被绑定的且未被拦截的属性对象 -->
        <div class="box" v-bind="$attrs">
            <h4>子组件2的第一个根元素</h4>
        </div>
        <div class="box" v-bind="$attrs">
            <h4>子组件2的第二个根元素</h4>
        </div>
    </script>

    <script type="module"> 
        import { createApp } from "../../assets/vue3/vue.esm-browser.js";
        const app = createApp({
            data(){
                return {
                    msg:"父组件变量msg"
                }
            }
        })
        app.component("CompOne",{
            template:"#compOne",
            props:["msg","class","style"]
        })
        app.component("SwitchBtn",{
            template:"#switchBtn",
            data(){
                return {
                    switchState:false
                }
            },
            methods:{
                changeState(state){
                    this.switchState = state;
                }
            }
        })
        app.component("CompTwo",{
            template:"#compTwo",
            props:["msg"]
        })
        app.mount("#app")
    </script>
    <!-- <script type="module"> 
        import Vue from "../../assets/vue2/vue.esm.browser.js";
        Vue.component("CompOne",{
            template:"#compOne",
            props:["msg","class","style"]
        })
        Vue.component("SwitchBtn",{
            template:"#switchBtn",
            data(){
                return {
                    switchState:false
                }
            },
            methods:{
                changeState(state){
                    this.switchState = state;
                }
            }
        })
        Vue.component("CompTwo",{
            template:"#compTwo",
        })
        new Vue({
            el:"#app",
            data(){
                return {
                    msg:"父组件变量msg"
                }
            }
        })
    </script> -->
</body>
</html>